home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2005 March / Macworld CD March 2005 - Marathon Trilogy.iso / Shareware World / iPod / iPodderX.sit / iPodderX / iPodderX.app / Contents / Resources / Storage.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2005-01-07  |  8.5 KB  |  232 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.3)
  3.  
  4. from sha import sha
  5. from bisect import bisect_right
  6.  
  7. class Storage:
  8.     
  9.     def __init__(self, files, open, exists, getsize):
  10.         self.ranges = []
  11.         total = 0x0L
  12.         so_far = 0x0L
  13.         for file, length in files:
  14.             if length != 0:
  15.                 self.ranges.append((total, total + length, file))
  16.                 total += length
  17.                 if exists(file):
  18.                     l = getsize(file)
  19.                     if l > length:
  20.                         l = length
  21.                     
  22.                     so_far += l
  23.                 
  24.             exists(file)
  25.             if not exists(file):
  26.                 open(file, 'wb').close()
  27.                 continue
  28.         
  29.         self.begins = [ i[0] for i in self.ranges ]
  30.         self.total_length = total
  31.         self.handles = { }
  32.         self.whandles = { }
  33.         self.tops = { }
  34.         for file, length in files:
  35.             if exists(file):
  36.                 l = getsize(file)
  37.                 self.tops[file] = l
  38.                 continue
  39.             None if l != length else []
  40.             self.handles[file] = open(file, 'wb+')
  41.             self.whandles[file] = 1
  42.         
  43.  
  44.     
  45.     def was_preallocated(self, pos, length):
  46.         for file, begin, end in self._intervals(pos, length):
  47.             if self.tops.get(file, 0) < end:
  48.                 return False
  49.                 continue
  50.         
  51.         return True
  52.  
  53.     
  54.     def set_readonly(self):
  55.         for file in self.whandles.keys():
  56.             old = self.handles[file]
  57.             old.flush()
  58.             old.close()
  59.             self.handles[file] = open(file, 'rb')
  60.         
  61.  
  62.     
  63.     def get_total_length(self):
  64.         return self.total_length
  65.  
  66.     
  67.     def _intervals(self, pos, amount):
  68.         r = []
  69.         stop = pos + amount
  70.         p = bisect_right(self.begins, pos) - 1
  71.         while p < len(self.ranges) and self.ranges[p][0] < stop:
  72.             (begin, end, file) = self.ranges[p]
  73.             r.append((file, max(pos, begin) - begin, min(end, stop) - begin))
  74.             p += 1
  75.         return r
  76.  
  77.     
  78.     def read(self, pos, amount):
  79.         r = []
  80.         for file, pos, end in self._intervals(pos, amount):
  81.             h = self.handles[file]
  82.             h.seek(pos)
  83.             r.append(h.read(end - pos))
  84.         
  85.         return ''.join(r)
  86.  
  87.     
  88.     def write(self, pos, s):
  89.         total = 0
  90.         for file, begin, end in self._intervals(pos, len(s)):
  91.             if not self.whandles.has_key(file):
  92.                 self.handles[file].close()
  93.                 self.handles[file] = open(file, 'rb+')
  94.                 self.whandles[file] = 1
  95.             
  96.             h = self.handles[file]
  97.             h.seek(begin)
  98.             h.write(s[total:total + end - begin])
  99.             total += end - begin
  100.         
  101.  
  102.     
  103.     def close(self):
  104.         for h in self.handles.values():
  105.             h.close()
  106.         
  107.  
  108.  
  109.  
  110. def lrange(a, b, c):
  111.     r = []
  112.     while a < b:
  113.         r.append(a)
  114.         a += c
  115.     return r
  116.  
  117. from fakeopen import FakeOpen
  118.  
  119. def test_Storage_simple():
  120.     f = FakeOpen()
  121.     m = Storage([
  122.         ('a', 5)], f.open, f.exists, f.getsize)
  123.     if not f.files.keys() == [
  124.         'a']:
  125.         raise AssertionError
  126.     m.write(0, 'abc')
  127.     if not m.read(0, 3) == 'abc':
  128.         raise AssertionError
  129.     m.write(2, 'abc')
  130.     if not m.read(2, 3) == 'abc':
  131.         raise AssertionError
  132.     m.write(1, 'abc')
  133.     if not m.read(0, 5) == 'aabcc':
  134.         raise AssertionError
  135.  
  136.  
  137. def test_Storage_multiple():
  138.     f = FakeOpen()
  139.     m = Storage([
  140.         ('a', 5),
  141.         ('2', 4),
  142.         ('c', 3)], f.open, f.exists, f.getsize)
  143.     x = f.files.keys()
  144.     x.sort()
  145.     if not x == [
  146.         '2',
  147.         'a',
  148.         'c']:
  149.         raise AssertionError
  150.     m.write(3, 'abc')
  151.     if not m.read(3, 3) == 'abc':
  152.         raise AssertionError
  153.     m.write(5, 'ab')
  154.     if not m.read(4, 3) == 'bab':
  155.         raise AssertionError
  156.     m.write(3, 'pqrstuvw')
  157.     if not m.read(3, 8) == 'pqrstuvw':
  158.         raise AssertionError
  159.     m.write(3, 'abcdef')
  160.     if not m.read(3, 7) == 'abcdefv':
  161.         raise AssertionError
  162.  
  163.  
  164. def test_Storage_zero():
  165.     f = FakeOpen()
  166.     Storage([
  167.         ('a', 0)], f.open, f.exists, f.getsize)
  168.     if not f.files == {
  169.         'a': [] }:
  170.         raise AssertionError
  171.  
  172.  
  173. def test_resume_zero():
  174.     f = FakeOpen({
  175.         'a': '' })
  176.     Storage([
  177.         ('a', 0)], f.open, f.exists, f.getsize)
  178.     if not f.files == {
  179.         'a': [] }:
  180.         raise AssertionError
  181.  
  182.  
  183. def test_Storage_with_zero():
  184.     f = FakeOpen()
  185.     m = Storage([
  186.         ('a', 3),
  187.         ('b', 0),
  188.         ('c', 3)], f.open, f.exists, f.getsize)
  189.     m.write(2, 'abc')
  190.     if not m.read(2, 3) == 'abc':
  191.         raise AssertionError
  192.     x = f.files.keys()
  193.     x.sort()
  194.     if not x == [
  195.         'a',
  196.         'b',
  197.         'c']:
  198.         raise AssertionError
  199.     if not len(f.files['a']) == 3:
  200.         raise AssertionError
  201.     if not len(f.files['b']) == 0:
  202.         raise AssertionError
  203.  
  204.  
  205. def test_Storage_resume():
  206.     f = FakeOpen({
  207.         'a': 'abc' })
  208.     m = Storage([
  209.         ('a', 4)], f.open, f.exists, f.getsize)
  210.     if not f.files.keys() == [
  211.         'a']:
  212.         raise AssertionError
  213.     if not m.read(0, 3) == 'abc':
  214.         raise AssertionError
  215.  
  216.  
  217. def test_Storage_mixed_resume():
  218.     f = FakeOpen({
  219.         'b': 'abc' })
  220.     m = Storage([
  221.         ('a', 3),
  222.         ('b', 4)], f.open, f.exists, f.getsize)
  223.     x = f.files.keys()
  224.     x.sort()
  225.     if not x == [
  226.         'a',
  227.         'b']:
  228.         raise AssertionError
  229.     if not m.read(3, 3) == 'abc':
  230.         raise AssertionError
  231.  
  232.